module writeArbiter (
    input wire write_en_1,
    input wire [5:0] data_priority_1,
    input wire [5:0] data_wait_time_1,
    input wire write_en_2,
    input wire [5:0] data_priority_2,
    input wire [5:0] data_wait_time_2,
    input wire write_en_3,
    input wire [5:0] data_priority_3,
    input wire [5:0] data_wait_time_3,
    input wire write_en_4,
    input wire [5:0] data_priority_4,
    input wire [5:0] data_wait_time_4,
    output wire [3:0] write_arbitration
);
    //比较器数组
    wire [6:0] priority_bigger;
    wire [3:0] priority_arbitration;
    wire priority_disable;
    
    //构建数组
    assign priority_bigger[0] = data_priority_1 >= data_priority_2;  //0-1
    assign priority_bigger[1] = data_priority_1 >= data_priority_3;  //0-2
    assign priority_bigger[2] = data_priority_1 >= data_priority_4;  //0-3
    assign priority_bigger[3] = data_priority_2 >= data_priority_3;  //1-2
    assign priority_bigger[4] = data_priority_2 >= data_priority_4;  //1-3
    assign priority_bigger[5] = data_priority_3 >= data_priority_4;  //2-3

    //根据优先级的值选择最大的那一个
    assign priority_arbitration[0] = write_en_1 & priority_bigger[0] & priority_bigger[1] & priority_bigger[2];
    assign priority_arbitration[1] = write_en_2 & ~priority_bigger[0] & priority_bigger[3] & priority_bigger[4];
    assign priority_arbitration[2] = write_en_3 & ~priority_bigger[1] & ~priority_bigger[3] & priority_bigger[5];
    assign priority_arbitration[3] = write_en_4 & ~priority_bigger[2] & ~priority_bigger[4] & ~priority_bigger[5];
    assign priority_disable = ~priority_bigger[0] & ~priority_bigger[1] & ~priority_bigger[2] & ~priority_bigger[3] & ~priority_bigger[4] & ~priority_bigger[5];

    //比较器数组
    wire [6:0] wait_time_bigger;
    wire [3:0] wait_time_arbitration;
    
    //构建数组
    assign wait_time_bigger[0] = data_wait_time_1 >= data_wait_time_2;  //0-1
    assign wait_time_bigger[1] = data_wait_time_1 >= data_wait_time_3;  //0-2
    assign wait_time_bigger[2] = data_wait_time_1 >= data_wait_time_4;  //0-3
    assign wait_time_bigger[3] = data_wait_time_2 >= data_wait_time_3;  //1-2
    assign wait_time_bigger[4] = data_wait_time_2 >= data_wait_time_4;  //1-3
    assign wait_time_bigger[5] = data_wait_time_3 >= data_wait_time_4;  //2-3

    //根据等待时间的值选择最大的那一个
    assign wait_time_arbitration[0] = write_en_1 & wait_time_bigger[0] & wait_time_bigger[1] & wait_time_bigger[2];
    assign wait_time_arbitration[1] = write_en_2 & ~wait_time_bigger[0] & wait_time_bigger[3] & wait_time_bigger[4];
    assign wait_time_arbitration[2] = write_en_3 & ~wait_time_bigger[1] & ~wait_time_bigger[3] & wait_time_bigger[5];
    assign wait_time_arbitration[3] = write_en_4 & ~wait_time_bigger[2] & ~wait_time_bigger[4] & ~wait_time_bigger[5];

    //如果优先级不等，则根据优先级顺序读取，如果优先级相等，则根据等待时间的长短来决定使用哪一个
    //assign write_arbitration = priority_disable ? wait_time_arbitration : priority_arbitration;
    //先简单处理下
    assign write_arbitration = priority_arbitration;
endmodule